package vip.ola.code.ctrl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ola.code.common.ctrl.BaseController;
import vip.ola.code.common.service.RpcCommonService;
import vip.ola.code.secruity.JwtUser;
import vip.ola.core.common.annotation.MethodLog;
import vip.ola.core.common.constant.Constant;
import vip.ola.core.common.constant.RetEnum;
import vip.ola.core.common.domain.BizResponse;
import vip.ola.core.common.domain.PayResponse;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.entity.code.PassageSettRecord;

import java.math.BigDecimal;

import static vip.ola.core.common.constant.Constant.*;

@RestController
@RequestMapping(Constant.CODE_CONTROLLER_ROOT_PATH + "/sett")
@PreAuthorize("hasRole('" + CODE_ROLE_NORMAL + "')")
public class SettController extends BaseController {

    private final MyLog log = MyLog.getLog(SettController.class);

    @Autowired
    private RpcCommonService rpcService;

    @RequestMapping("/list")
    public ResponseEntity<?> list(PassageSettRecord settRecord) {
        settRecord.setPassageId(getUser().getId());
        IPage<PassageSettRecord> page = rpcService.passageSettRecordService.list(getPageIndex(), getPageSize(), settRecord);
        return ResponseEntity.ok(PayResponse.buildPageSuccess(page));
    }

    @RequestMapping("/get")
    @MethodLog(remark = "结算详情")
    public ResponseEntity get(long id) {
        JwtUser user = getUser();
        PassageSettRecord record = rpcService.passageSettRecordService.getById(id);
        if (!user.getId().equals(record.getPassageId())) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_AUTH));
        }
        return ResponseEntity.ok(PayResponse.buildSuccess(record));
    }

    @RequestMapping("/remit")
    @MethodLog(remark = "结算打款")
    public ResponseEntity remit(long id, int status, String remitRemark) {
        JwtUser user = getUser();
        PassageSettRecord record = rpcService.passageSettRecordService.getById(id);
        if (!SETT_STATUS_REMIT_SUCCESS.equals(status) && !SETT_STATUS_REMIT_FAIL.equals(status)) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_AUTH));
        }
        if (record.getStatus().equals(status)) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_REPETITIVE_OPERATION));
        }
        if (!user.getId().equals(record.getPassageId())) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_AUTH));
        }
        record.setStatus(status).setRemitRemark(remitRemark);
        rpcService.passageSettRecordService.remit(record);
        return ResponseEntity.ok(PayResponse.buildSuccess(record));
    }

}
